/*
 * @Author: Warren
 * @Date: 2025-02-23 16:11:08
 * @LastEditors: Warren
 * @LastEditTime: 2025-06-09 14:27:43
 * @FilePath: /nest-web/src/interceptors/response/response.interceptor.ts
 * @Description: 拦截器
 */
import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';
import { ResponseFormat } from './response.interface';
import { Observable, map } from 'rxjs';

@Injectable()
export class ResponseInterceptor<T> implements NestInterceptor<T, ResponseFormat<T>> {
    intercept(context: ExecutionContext, next: CallHandler): Observable<ResponseFormat<T>> {
        const ctx = context.switchToHttp();
        const response = ctx.getResponse();
        // const request = ctx.getRequest();

        return next.handle().pipe(
            map((data) => ({
                code: 1,
                // code: response.statusCode,
                msg: response.msg || 'success',
                data: data || null,
            }))
        )
        
    }
}